<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/22
 * Time: 17:22
 */

namespace Auditing\Model;


use Think\Model;
use User\Model\UserModel;

class DoctorModel extends Model
{
    //自动验证
    protected $patchValidate = true;//开启批量验证
    protected $_validate = [
        ['name','require','姓名不能为空'],
        ['phone','require','电话不能为空'],
        ['id_no','require','身份证号码不能为空'],
        ['are_id_head','require','必须选择地区'],
        ['phone','/^1[34578]\d{9}$/','手机输入不合法',self::EXISTS_VALIDATE,'regex',self::MODEL_BOTH],
        ['id_no','number','身份证输入不合法'],
        ['id_no','18','身份证号码不合法',self::EXISTS_VALIDATE,'length'],
    ];
    //自动完成
    protected $_auto = [
        ['start_time','time',self::MODEL_INSERT,'function'],
    ];

    /**
     * 添加到医生审核到数据表
     * @return bool
     */
    public function addDoctor(){
        $user_token = session('NOW_USER_TOKEN');
        $user_info = M('userdetail')->where(['token'=>$user_token])->find();
        //获取数据
        $data = I('post.');
        $user = new UserModel();
        //判断当前用户是否已经提交过审核
        $user_id = $user->getIdByToken($user_token);
        $res = $this->where(['user_id'=>$user_id])->find();
        if($res){
            $this->error = '提交失败，请误重新提交审核';
            return false;
        }
        //查询是否当前用户在doctor_fail表中已存在
        //如果存在 则删除 即可以重新提交
        M('DoctorFail')->where(['user_id'=>$user_id])->delete();

        //对自定义输入的技能进行分割
        $skills = $data['skill'];
        $skills = str_replace('，',',',$skills);
        $skills = str_replace('、',',',$skills);
        $skills = str_replace('。',',',$skills);
        $skills = str_replace(' ',',',$skills);
        $skills = trim($skills,',');//去掉两边得，
        $this->skill = $skills;
        $this->headurl = $user_info['headurl'];
        $this->user_id = $user_info['userid'];
        //要加入的团队
        $group_label = I('post.group_label');
        $group = M('GroupTable')->field('id,name')->where(['label'=>$group_label])->find();
        $this->group_name = $group['name'];
        $this->group_id = $group['id'];
        //添加到数据库
        if(($doctor_id = $this->add()) === false){
            $this->error = '提交失败，请重新提交';
            return false;
        }
        //上传图片 获取对应地址
        $paths = uploadDoctor();
        $data = [];
        foreach ($paths as $k=>$path){
            switch ($k){
                case 'img_idz':
                    $data[] = [
                        'doc_id'=>$doctor_id,
                        'flag'=>1,
                        'url'=>$path,
                    ];
                    break;
                case 'img_idf':
                    $data[] = [
                        'doc_id'=>$doctor_id,
                        'flag'=>2,
                        'url'=>$path,
                    ];
                    break;
                case 'img_edu':
                    $data[] = [
                        'doc_id'=>$doctor_id,
                        'flag'=>3,
                        'url'=>$path,
                    ];
                    break;
                case 'img_skill':
                    foreach ($path as $item){
                        $data[] = [
                            'doc_id'=>$doctor_id,
                            'flag'=>4,
                            'url'=>$item,
                        ];
                    }
                    break;
                case 'img_job':
                    $data[] = [
                        'doc_id'=>$doctor_id,
                        'flag'=>5,
                        'url'=>$path,
                    ];
                    break;
            }
        }
        if(M('DoctorImg')->addAll($data) === false){
            $this->error = '图片上传失败';
            return false;
        }
        return true;
    }

    /**
     * 修改医生资料
     */
    public function editDoctor($data){
        //判断是否为非法操作
        $user_token = session('NOW_USER_TOKEN');//获取用户信息
        $user_id = M('userdetail')->where(['token'=>$user_token])->getField('userid');
        $dor_id = M('Doctor')->where(['user_id'=>$user_id])->getField('id');
        if($data['id'] != $dor_id){
            return ['status'=>0,'msg'=>'非法操作！'];
        }
        //首先验证数据合法性
        if(!preg_match('/^1[34578]\d{9}$/',$data['phone'])){
            return ['status'=>0,'msg'=>'手机号码不合法'];
        }
        //对自定义输入的技能进行分割
        $skills = $data['skill'];
        $skills = str_replace('，',',',$skills);
        $skills = str_replace('、',',',$skills);
        $skills = str_replace('。',',',$skills);
        $skills = str_replace(' ',',',$skills);
        $skills = trim($skills,',');//去掉两边得，
        $data['skill'] = $skills;
        if($this->save($data) === false){
            return ['status'=>0,'msg'=>'修改失败，请再次尝试'];
        }
        return ['status'=>1,'msg'=>'修改成功'];
    }

    /**
     * 根据用户token获取当前用户的审核状态
     * 0待审核 1成功
     * @param $token
     * @return mixed
     */
    public function getDoctorStatus($token){
        $user_model = new UserModel();
        $user_id = $user_model->getIdByToken($token);
        $res = $this->where(['user_id'=>$user_id])->field('status')->find();
        return $res['status'];
    }

    /**
     * 根据用户token查询出是否该用户已被拒绝审核
     * @param $token
     * @return mixed
     */
    public function queryDoctorFail($token){
        $user_model = new UserModel();
        $user_id = $user_model->getIdByToken($token);
        return M('DoctorFail')->where(['user_id'=>$user_id])->find();
    }
}